package day03.text01;

import java.io.*;
import java.net.Socket;

public class MyRunbale implements Runnable {
    private Socket accept;
    public MyRunbale(Socket accept) {
        this.accept=accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bo = null;
        try {
            bo = new BufferedOutputStream(new FileOutputStream("D:\\Java\\group5InClass27\\" + System.currentTimeMillis() + ".txt"));

            InputStream inputStream = accept.getInputStream();
            BufferedInputStream bi = new BufferedInputStream(inputStream);
            //通过网络读取文件信息并写入文件中

            int i;
            while ((i = bi.read()) != -1) {
                bo.write(i);
            }
            //返回一个消息给客服端
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bo!=null){
                try {
                    bo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           if(accept!=null) {
               try {
                   accept.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }



    }
}
